/**
 * @author yxw
 * date : 2013年11月18日 下午4:17:19 
 */
package com.tzinfo.tzt.service;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;

import com.tzinfo.tzt.config.AppDataConfig;
import com.tzinfo.tzt.config.FileConfig;
import com.tzinfo.tzt.domain.SeeNote;
import com.tzinfo.tzt.util.DBHelper;
import com.tzinfo.tzt.util.FileTools;
import com.tzinfo.tzt.util.StringTools;

public class HouseSeeNoteService {

	Context context;

	public HouseSeeNoteService(Context context) {
		this.context = context;
	}

	public boolean deleteAll() {
		// 删除SD卡中图片
		List<SeeNote> list = getSeeNoteList();
		for (int i = 0; i < list.size(); i++) {
			this.deleteImageOnSD(list.get(i).getImages());
		}
		return DBHelper.getInstance(context).deleteAll(AppDataConfig.SeeNote);
	}

	public boolean deleteSeeNoteById(String id) {
		// 删除SD卡中图片
		this.deleteImageOnSD(getSeeNoteById(id).getImages());
		int flag = DBHelper.getInstance(context).deleteById(
				AppDataConfig.SeeNote, id);
		return flag > 0 ? true : false;
	}

	public List<SeeNote> getSeeNoteList() {
		Cursor cursor = DBHelper.getInstance(context).query(
				AppDataConfig.SeeNote, "lastModifyTime", "desc");
		List<SeeNote> list = new ArrayList<SeeNote>();

		while (cursor.moveToNext()) {
			SeeNote item = new SeeNote();
			item.setId(cursor.getInt(cursor.getColumnIndex("_id")));
			item.setHouseid(cursor.getInt(cursor.getColumnIndex("houseid")));
			item.setVillageName(cursor.getString(cursor
					.getColumnIndex("villageName")));
			item.setVillageAddress(cursor.getString(cursor
					.getColumnIndex("villageAddress")));
			item.setPrice(cursor.getString(cursor.getColumnIndex("price")));
			item.setType(cursor.getString(cursor.getColumnIndex("type")));
			item.setSize(cursor.getInt(cursor.getColumnIndex("size")));
			item.setFloor(cursor.getString(cursor.getColumnIndex("floor")));
			item.setDirection(cursor.getString(cursor
					.getColumnIndex("direction")));
			item.setRemark(cursor.getString(cursor.getColumnIndex("remark")));
			item.setAgentName(cursor.getString(cursor
					.getColumnIndex("agentName")));
			item.setAgentTel(cursor.getString(cursor.getColumnIndex("agentTel")));
			item.setDate(cursor.getString(cursor.getColumnIndex("date")));
			item.setTime(cursor.getString(cursor.getColumnIndex("time")));
			// 特殊处理
			String imagesString = cursor.getString(cursor
					.getColumnIndex("images"));
			String[] imagesUris = imagesString.split(";;");
			List<String> images = new ArrayList<String>();
			if (!StringTools.isEmpty(imagesUris[0])) {
				for (int i = 0; i < imagesUris.length; i++) {
					images.add(imagesUris[i]);
				}
			}
			item.setImages(images);
			item.setLastModifyTime(cursor.getString(cursor
					.getColumnIndex("lastModifyTime")));
			list.add(item);
		}
		cursor.close();
		return list;
	}

	public SeeNote getSeeNoteById(String id) {
		Cursor cursor = DBHelper.getInstance(context).query(
				AppDataConfig.SeeNote, id);
		SeeNote item = null;
		if (cursor.moveToNext()) {
			item = new SeeNote();
			item.setId(cursor.getInt(cursor.getColumnIndex("_id")));
			item.setHouseid(cursor.getInt(cursor.getColumnIndex("houseid")));
			item.setVillageName(cursor.getString(cursor
					.getColumnIndex("villageName")));
			item.setVillageAddress(cursor.getString(cursor
					.getColumnIndex("villageAddress")));
			item.setPrice(cursor.getString(cursor.getColumnIndex("price")));
			item.setType(cursor.getString(cursor.getColumnIndex("type")));
			item.setSize(cursor.getInt(cursor.getColumnIndex("size")));
			item.setFloor(cursor.getString(cursor.getColumnIndex("floor")));
			item.setDirection(cursor.getString(cursor
					.getColumnIndex("direction")));
			item.setRemark(cursor.getString(cursor.getColumnIndex("remark")));
			item.setAgentName(cursor.getString(cursor
					.getColumnIndex("agentName")));
			item.setAgentTel(cursor.getString(cursor.getColumnIndex("agentTel")));
			item.setDate(cursor.getString(cursor.getColumnIndex("date")));
			item.setTime(cursor.getString(cursor.getColumnIndex("time")));
			// 特殊处理
			String imagesString = cursor.getString(cursor
					.getColumnIndex("images"));
			String[] imagesUris = imagesString.split(";;");
			List<String> images = new ArrayList<String>();
			if (!StringTools.isEmpty(imagesUris[0])) {
				for (int i = 0; i < imagesUris.length; i++) {
					images.add(imagesUris[i]);
				}
			}
			item.setImages(images);
			item.setLastModifyTime(cursor.getString(cursor
					.getColumnIndex("lastModifyTime")));
		}
		cursor.close();
		return item;
	}

	public boolean saveSeeNote(SeeNote info) {
		ContentValues value = new ContentValues();
		if (!"0".equals(info.getId() + "")) {
			value.put("_id", info.getId());
		}
		if (!"0".equals(info.getHouseid() + "")) {
			value.put("houseid", info.getHouseid());
		}
		value.put("villageName", info.getVillageName());
		value.put("villageAddress", info.getVillageAddress());
		value.put("price", info.getPrice());
		value.put("type", info.getType());
		if (!"0".equals(info.getSize() + "")) {
			value.put("size", info.getSize());
		}
		value.put("floor", info.getFloor());
		value.put("direction", info.getDirection());
		value.put("remark", info.getRemark());
		value.put("agentName", info.getAgentName());
		value.put("agentTel", info.getAgentTel());
		value.put("date", info.getDate());
		value.put("time", info.getTime());
		List<String> images = info.getImages();
		String imagesString = "";
		if (images != null && images.size() > 0) {
			for (int i = 0; i < images.size(); i++) {
				imagesString += images.get(i) + ";;";
			}
			imagesString = imagesString.substring(0, imagesString.length() - 2);
		}
		value.put("images", imagesString);
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// 设置日期格式
		value.put("lastModifyTime", df.format(new Date()));// 获取当前时间
		long flag = DBHelper.getInstance(context).save(AppDataConfig.SeeNote,
				value);
		return flag > 0 ? true : false;
	}

	public boolean saveSeeNoteImages(SeeNote info) {
		ContentValues value = new ContentValues();
		if (!"0".equals(info.getId() + "")) {
			value.put("_id", info.getId());
		}
		if (!"0".equals(info.getHouseid() + "")) {
			value.put("houseid", info.getHouseid());
		}
		List<String> images = info.getImages();
		String imagesString = "";
		if (images != null && images.size() > 0) {
			for (int i = 0; i < images.size(); i++) {
				imagesString += images.get(i) + ";;";
			}
			imagesString = imagesString.substring(0, imagesString.length() - 2);
		}
		value.put("images", imagesString);
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// 设置日期格式
		value.put("lastModifyTime", df.format(new Date()));// 获取当前时间
		long flag = DBHelper.getInstance(context).save(AppDataConfig.SeeNote,
				value);
		return flag > 0 ? true : false;
	}

	private List<String> delList;

	public void deleteImageOnSD(List<String> list) {
		this.delList = list;
		if (list != null && list.size() > 0) {
			new Thread(deleteImagesRun).start();
		}
	}

	Runnable deleteImagesRun = new Runnable() {

		@Override
		public void run() {
			for (int j = 0; j < delList.size(); j++) {
				File dirFile = new File(delList.get(j));
				if (dirFile != null && dirFile.isFile()) {
					dirFile.delete();
				}
			}
		}
	};

}
